<?php

namespace Trawind\Basics\Traits;


use SwooleTW\Http\Coroutine\Context;

trait HasSingleton
{

    private static $instance;


    public static function getInstance(...$args)
    {
        if (Context::getRequestedCoroutineId() < 0) {
            if (!isset(self::$instance)) {
                self::$instance = new static(...$args);
            }
            return self::$instance;
        } else {
            if (is_null(Context::getData(static::class))) {
                Context::setData(static::class, new static(...$args));
            }
            return Context::getData(static::class);
        }
    }

}
